const { Util } = require("../../../utils/util");
const wxapi = require("../../../utils/wxapi");
const CryptoJS = require('../../../utils/crypto-js');
const { API_IMAGE_URL, API_SNZO_URL, API_SNZO_FILE_URL } = require("../../../utils/wxapi");
const app = getApp();
// pages/exchange/apply/apply.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: null,
    educationOfData: ['研究生', '大学本科', '大学专科', '专科学校', '中等专业学校', '中等技术学校', '技工学校', '高中', '初中', '小学',],
    educationIndex: null,
    projectOfData: [],
    sortOfData: [],
    exhibitsOfData: [],
    auctionOfData: [],
    userInfo: null,
    tabStnumArtPeopleMsg: null,
    showExhibits: false,
    showAuction: false,
    imgUrl: API_SNZO_FILE_URL,
    showError: false,
    isHandle: false,
    activityId: null,
    showExhibitsMore: true,
    applyType: null,
    exhibitsMoreNum: null,
    standby5: null,
    activityIntro: null,
    payMoney: null,
    nameSpecification: true
  },

  /**
   * 获取活动详情
   */
  getActivityIntro() {
    wxapi.selectArtActiveById({ id: this.data.activityId }).then(res => {
      if (res.status === 200) {
        this.setData({
          activityIntro: res.data
        })
        this.getWorksType(res.data.worksTypeId);
      }
    })
  },

  /**
   * 获取需要支付金额
   */
  getPayMoney() {
    const bodyParams = {
      tabStnumArtPeopleMsg: {
        activeId: this.data.activityId,
        stk: app.globalData.userInfo.cardNo,
      },
    }
    wxapi.artEnrollLsBufei(bodyParams).then(res => {
      if (res.status === 200) {
        this.setData({
          payMoney: res.msg
        })
      }
    })
  },

  /**
   * 获取用户基本信息
   */
  getUserInfo() {
    const allParams = {
      cardNo: app.globalData.userInfo.cardNo,
      timestamp: new Date().getTime()
    };
    const trimParams = Util.trimNulls(allParams);
    const pendSign = { ...trimParams, url: '/card/detail' };
    const sign = Util.jsonSort(pendSign);
    const md5Sign = CryptoJS.MD5(sign);
    const params = { sign: md5Sign, ...trimParams };
    const headers = {
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
    };
    wxapi.queryCardInfo(params, headers).then(res => {
      if (res.code === 20000) {
        const userInfo = res.data;
        const tabStnumArtPeopleMsg = {
          name: {
            value: userInfo.nickName,
            showError: false,
            disabled: userInfo.nickName ? true : false,
          },
          gender: {
            value: userInfo.sex,
            showError: false,
            disabled: userInfo.sex ? true : false,
          },
          birthDay: {
            value: userInfo.birthday,
            showError: false,
            disabled: userInfo.birthday ? true : false,
          },
          mostEducation: { value: null, showError: false, },
          country: { value: null, showError: false, },
          nation: { value: null, showError: false, },
          duties: { value: null, showError: false, },
          cardNum: {
            value: userInfo.certificateNo,
            showError: false,
            disabled: userInfo.certificateNo ? true : false,
          },
          phone: {
            value: userInfo.userPhone,
            showError: false,
            disabled: userInfo.userPhone ? true : false,
          },
          email: {
            value: userInfo.email,
            showError: false,
            disabled: userInfo.email ? true : false,
          },
          address: {
            value: `${userInfo.belongProvince}${userInfo.belongProvince === userInfo.belongCity
              ? ''
              : userInfo.belongCity
              }${userInfo.belongArea}${userInfo.addressDetail}`,
            showError: false,
            disabled: userInfo.belongProvince ? true : false,
          },
          unit: { value: null, showError: false, },
          item: { value: null, showError: false, },
          stk: { value: userInfo.cardNo },
        }
        this.setData({
          tabStnumArtPeopleMsg
        })
        app.globalData.userInfo = userInfo;
        if (this.data.applyType === 'upload') {
          this.artEnrollLsSum();
        } else {
          this.checkSignUpArt(userInfo)
        }
      } else if (res.code === 20005) {
        wx.showModal({
          title: '提示信息',
          content: res.message,
          confirmText: '登录',
          complete: (res) => {
            app.globalData.userInfo = null;
            app.globalData.token = null;
            wx.removeStorage({
              key: 'userInfo',
            })
            wx.removeStorage({
              key: 'token',
            })
            if (res.cancel) {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/login',
              })
            }
          }
        })
      }
    })
  },

  /**
   * 校验用户报名状态及已上传艺术品数量
   */
  artEnrollLsSum() {
    const bodyParams = {
      tabStnumArtPeopleMsg: {
        activeId: this.data.activityId,
        stk: app.globalData.userInfo.cardNo,
      },
    };
    wxapi.artEnrollLsSum(bodyParams).then(res => {
      if (res.status === 200) {
        if (Number(res.msg) >= 15) {
          wx.showModal({
            title: '提示信息',
            content: '您上传的艺术品不能大于15副。',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                wx.navigateTo({
                  url: `/pages/exchange/detail/detail?activityId=${this.data.activityId}`,
                })
              }
            }
          })
        } else {
          this.exhibitsMoreNum = 15 - Number(res.msg);
        }
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: `/pages/exchange/detail/detail?activityId=${this.data.activityId}`,
              })
            }
          }
        })
      }
    })
  },

  /**
   * 校验用户是否已经报名
   * @param {*} userInfo 
   */
  checkSignUpArt(userInfo) {
    const bodyParams = {
      stk: userInfo.cardNo,
      activityId: this.data.activityId
    }
    wxapi.checkSignUpArt(bodyParams).then(res => {
      if (res.status === 200) {
        this.setData({
          activityId: this.data.activityId
        })
        this.getProject(this.data.activityId);
        this.getWorksType(this.data.activityId);
        if (this.data.activityId == 26) {
          this.addExhibits();
          this.setData({
            showExhibits: true
          })
        }
      } else if (res.status === 400) {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/exchange/exchange',
              })
            }
          }
        })
      }
    })
  },

  /**
   * 日期选择
   * @param {*} e 
   */
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  bindEducationChange(e) {
    this.setData({
      educationIndex: e.detail.value
    })
  },

  /**
   * 选择参加项目
   * @param {*} e 
   */
  projectChange(e) {
    const values = e.detail.value;
    if (values.includes('22')) {
      this.setData({
        showExhibits: true
      })
      if (this.data.exhibitsOfData.length === 0) this.addExhibits();
    } else {
      this.setData({
        showExhibits: false,
        exhibitsOfData: []
      })
    }

    if (values.includes('10') || values.includes('11') || values.includes('12')) {
      this.setData({
        showAuction: true
      })
      if (this.data.auctionOfData.length === 0) this.addAuction();
    } else {
      this.setData({
        showAuction: false,
        auctionOfData: []
      })
    }
    // this.data.tabStnumArtPeopleMsg.item.value = values.join('|');
  },

  /**
   * 校验艺术品名称
   * @param {*} e 
   */
  worksNameOnBlur(e) {
    if (this.data.activityIntro.contest) {
      const worksName = e.detail.value;
      const worksNameEnding = worksName.substring(worksName.length - this.data.activityIntro.contest.length);
      if (worksNameEnding !== this.data.activityIntro.contest) {
        this.data.nameSpecification = false;
        wx.showModal({
          title: '提示信息',
          content: `艺术品称谓后需要加注${this.data.activityIntro.contest}`,
          showCancel: false
        })
      } else {
        this.data.nameSpecification = true;
      }
    }
  },

  /**
   * 输入名称
   * @param {*} e 
   */
  exhibitsNameChange(e) {
    const exhibitsIndex = e.currentTarget.dataset.exhibitsIndex;
    const exhibitsName = e.detail.value;
    this.data.exhibitsOfData[exhibitsIndex].name = exhibitsName;
    this.setData({
      exhibitsOfData: this.data.exhibitsOfData
    })
  },

  /**
   * 输入名称
   * @param {*} e 
   */
  auctionNameChange(e) {
    const auctionIndex = e.currentTarget.dataset.auctionIndex;
    const auctionName = e.detail.value;
    this.data.auctionOfData[auctionIndex].name = auctionName;
    this.setData({
      auctionOfData: this.data.auctionOfData
    })
  },

  /**
   * 输入作者
   * @param {*} e 
   */
  exhibitsAuthorChange(e) {
    const exhibitsIndex = e.currentTarget.dataset.exhibitsIndex;
    const exhibitsAuthor = e.detail.value;
    this.data.exhibitsOfData[exhibitsIndex].author = exhibitsAuthor;
    this.setData({
      exhibitsOfData: this.data.exhibitsOfData
    })
  },

  /**
   * 输入作者
   * @param {*} e 
   */
  auctionAuthorChange(e) {
    const auctionIndex = e.currentTarget.dataset.auctionIndex;
    const auctionAuthor = e.detail.value;
    this.data.auctionOfData[auctionIndex].author = auctionAuthor;
    this.setData({
      auctionOfData: this.data.auctionOfData
    })
  },

  /**
   * 多选主题选择
   * @param {*} e 
   */
  checkboxThemeChange(e) {
    const themeType = e.currentTarget.dataset.themeType;
    const themeIndex = e.currentTarget.dataset.themeIndex;
    const values = e.detail.value;
    if (themeType === 'exhibits') {
      this.data.exhibitsOfData[themeIndex].showMajor = values.join('|');
    } else if (themeType === 'auction') {
      this.data.auctionOfData[themeIndex].showMajor = values.join('|');
    }
  },

  /**
   * 单选主题选择
   * @param {*} e 
   */
  radioThemeChange(e) {
    const themeType = e.currentTarget.dataset.themeType;
    const themeIndex = e.currentTarget.dataset.themeIndex;
    const values = e.detail.value;
    if (themeType === 'exhibits') {
      this.data.exhibitsOfData[themeIndex].showMajor = values;
    } else if (themeType === 'auction') {
      this.data.auctionOfData[themeIndex].showMajor = values;
    }
  },

  /**
   * 输入长度
   * @param {*} e 
   */
  longChange(e) {
    const longIndex = e.currentTarget.dataset.acreageIndex;
    const longValue = e.detail.value;
    const regex = /^[1-9]\d*$/;
    if (!longValue) {
      this.data.exhibitsOfData[longIndex].showLong = '';
    } else if (!isNaN(+longValue) && regex.test(longValue.toString())) {
      this.data.exhibitsOfData[longIndex].showLong = longValue;
      if (this.data.exhibitsOfData[longIndex].showWide) {
        this.data.exhibitsOfData[longIndex].showAcreage = this.data.exhibitsOfData[longIndex].showWide * Number(longValue);
        this.setData({
          exhibitsOfData: this.data.exhibitsOfData
        })
      }
    } else {
      this.data.exhibitsOfData[longIndex].showLong = '';
      this.data.exhibitsOfData[longIndex].showAcreage = '';
    }
    this.setData({
      exhibitsOfData: this.data.exhibitsOfData
    })
  },

  /**
   * 输入宽度
   * @param {*} e 
   */
  wideChange(e) {
    const wideIndex = e.currentTarget.dataset.acreageIndex;
    const wideValue = e.detail.value;
    const regex = /^[1-9]\d*$/;
    if (!wideValue) {
      this.data.exhibitsOfData[wideIndex].showWide = '';
    } else if (!isNaN(+wideValue) && regex.test(wideValue.toString())) {
      this.data.exhibitsOfData[wideIndex].showWide = wideValue;
      if (this.data.exhibitsOfData[wideIndex].showLong) {
        this.data.exhibitsOfData[wideIndex].showAcreage =
          this.data.exhibitsOfData[wideIndex].showLong * Number(wideValue);
      }
    } else {
      this.data.exhibitsOfData[wideIndex].showWide = '';
      this.data.exhibitsOfData[wideIndex].showAcreage = '';
    }
    this.setData({
      exhibitsOfData: this.data.exhibitsOfData
    })
  },

  /**
   * 拍卖艺术品长
   * @param {*} e 
   */
  auctionLongChange(e) {
    const longValue = e.detail.value;
    const longIndex = e.currentTarget.dataset.acreageIndex;
    const regex = /^[1-9]\d*$/;
    if (!longValue) {
      this.data.auctionOfData[longIndex].showLong = '';
    } else if (!isNaN(+longValue) && regex.test(longValue.toString())) {
      this.data.auctionOfData[longIndex].showLong = longValue;
    } else {
      this.data.auctionOfData[longIndex].longValue = '';
    }
    this.setData({
      auctionOfData: this.data.auctionOfData
    })
  },

  /**
   * 拍卖艺术品宽
   * @param {*} e 
   */
  auctionWideChange(e) {
    const wideIndex = e.currentTarget.dataset.acreageIndex;
    const wideValue = e.detail.value;
    const regex = /^[1-9]\d*$/;
    if (!wideValue) {
      this.data.auctionOfData[wideIndex].showWide = '';
    } else if (!isNaN(+wideValue) && regex.test(wideValue.toString())) {
      this.data.auctionOfData[wideIndex].showWide = wideValue;
    } else {
      this.data.auctionOfData[wideIndex].showWide = '';
    }
    this.setData({
      auctionOfData: this.data.auctionOfData
    })
  },

  /**
   * 上传艺术品
   * @param {*} e 
   */
  uploadWorks(e) {
    const worksIndex = e.currentTarget.dataset.worksIndex;
    const worksType = e.currentTarget.dataset.worksType;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: (res) => {
        const filePath = res.tempFiles[0].tempFilePath;
        const thumbSize = (res.tempFiles[0].size / 1024 / 1024).toFixed(2);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          filePath,
          name: 'file',
          url: API_SNZO_URL + '/upload/uploadImg',
          success: (result) => {
            wx.hideLoading()
            const uploadResult = JSON.parse(result.data);
            if (uploadResult.status === 200) {
              if (worksType === 'exhibits') {
                this.data.exhibitsOfData[worksIndex].showPath = uploadResult.data;
                this.data.exhibitsOfData[worksIndex].worksStorage = thumbSize;
                this.setData({
                  exhibitsOfData: this.data.exhibitsOfData
                })
              } else if (worksType === 'auction') {
                this.data.auctionOfData[worksIndex].showPath = uploadResult.data;
                this.data.auctionOfData[worksIndex].worksStorage = thumbSize;
                this.setData({
                  auctionOfData: this.data.auctionOfData
                })
              }
            } else {
              wx.showModal({
                title: '提示信息',
                content: result.msg,
                showCancel: false
              })
            }
          },
          fail: (result) => {
            wx.hideLoading()
          }
        })
      }
    })
  },

  /**
   * 添加控件
   * @param {*} e 
   */
  addControl(e) {
    const itemType = e.currentTarget.dataset.itemType;
    if (itemType === 'exhibits') {
      this.addExhibits()
    } else if (itemType === 'auction') {
      this.addAuction()
    }
  },

  /**
   * 删除控件
   * @param {*} e 
   */
  delControl(e) {
    const itemIndex = e.currentTarget.dataset.itemIndex;
    const itemType = e.currentTarget.dataset.itemType;
    if (itemType === 'exhibits') {
      this.data.exhibitsOfData.splice(itemIndex, 1)
      this.setData({
        exhibitsOfData: this.data.exhibitsOfData
      })
    } else if (itemType === 'auction') {
      this.data.auctionOfData.splice(itemIndex, 1)
      this.setData({
        auctionOfData: this.data.auctionOfData
      })
    }
  },

  /**
   * 获取参加项目
   */
  getProject(belongActiveid) {
    wxapi.getStnumArtItemList({ belongActiveid }).then(res => {
      if (res.status === 200) {
        if (belongActiveid == 24) {
          // const projectOfData = res.data.filter(item => {
          //   if (!item.itemName.includes('拍卖')) return item;
          // });
          const projectOfData = res.data;
          this.setData({
            projectOfData
          })
        }
        if (belongActiveid == 26) {
          const projectOfData = res.data.map(item => { item.checked = true; return item; });
          this.setData({
            projectOfData
          })
        }
      }
    })
  },

  /**
   * 获取专业
   */
  getWorksType(standby4) {
    wxapi.getStNumWorksType({ standby4 }).then(res => {
      if (res.status === 200) {
        const sortOfData = res.data.filter(item => { item.checked = false; return item });
        this.setData({
          sortOfData
        })
      }
    })
  },

  /**
   * 添加列展艺术品
   * @param e
   */
  addExhibits() {
    const control = {
      name: '',
      author: '',
      showMajor: '',
      worksStorage: '',
      showPath: '',
      showLong: '',
      showWide: '',
      standby1: '',
      standby2: '',
      standby3: '',
      standby4: '',
      standby5: this.data.standby5,
      specs: '',
      priceFee: '',
      showAcreage: '',
      fileName: '',
      relateActiveId: '',
      relatePeriodId: '',
      relateVenueId: '',
    };
    const exhibitsOfData = this.data.exhibitsOfData;
    exhibitsOfData.push(control);
    if (this.data.activityId == 26 && exhibitsOfData.length === 3) {
      this.setData({
        showExhibitsMore: false
      })
    }
    if ((this.data.activityId == 29 || this.data.activityId == 31) && this.data.exhibitsOfData.length == this.data.exhibitsMoreNum) {
      this.setData({
        showExhibitsMore: false
      })
    }
    this.setData({
      exhibitsOfData
    })
  },

  /**
   * 删除列展艺术品
   * @param index
   * @param e
   */
  removeExhibits(index) {
    const exhibitsOfData = this.data.exhibitsOfData;
    exhibitsOfData.splice(index, 1);
    this.setData({
      exhibitsOfData
    })
  },

  /**
   * 添加拍卖艺术品
   * @param e
   */
  addAuction() {
    const control = {
      name: '',
      author: '',
      showMajor: '',
      worksStorage: '',
      showPath: '',
      showLong: '',
      showWide: '',
      standby1: '',
      standby2: '',
      standby3: '',
      standby4: '',
      standby5: 2,
      specs: '',
      priceFee: '',
      showAcreage: '',
      fileName: '',
      relateActiveId: '',
      relatePeriodId: '',
      relateVenueId: '',
    };
    const auctionOfData = this.data.auctionOfData;
    auctionOfData.push(control);
    this.setData({
      auctionOfData
    })
  },

  /**
   * 删除拍卖艺术品
   * @param index
   * @param e
   */
  removeAuction(index) {
    const auctionOfData = this.data.auctionOfData;
    auctionOfData.splice(index, 1);
    this.setData({
      auctionOfData
    })
  },

  /**
   * 查看交易会规程
   */
  openRule() {
    wx.downloadFile({
      url: 'https://aa.snzo.cn/assets/files/jyhgc.pdf',
      success: (res) => {
        const filePath = res.tempFilePath;
        wx.openDocument({
          filePath
        })
      }
    })
  },

  /**
   * 提交报名
   * @param {*} e 
   */
  formSubmit(e) {
    if (this.data.isHandle) {
      return;
    }
    this.data.isHandle = true;
    if (this.data.applyType === 'upload') {
      if (this.data.activityId == 29 || this.data.activityId == 31) {
        for (const item of this.data.exhibitsOfData) {
          if (!item.name) {
            wx.showModal({
              title: '提示信息',
              content: '请填写列展艺术品名称。',
              showCancel: false
            })
            return;
          }
          if (item.name.substring(item.name.length - this.data.activityIntro.contest.length) != this.data.activityIntro.contest) {
            wx.showModal({
              title: '提示信息',
              content: '列展艺术品名称不符合规范。',
              showCancel: false
            })
            return;
          }
          if (!item.author) {
            wx.showModal({
              title: '提示信息',
              content: '请上传列展艺术品作者。',
              showCancel: false
            })
            return;
          }
          if (!item.showPath) {
            wx.showModal({
              title: '提示信息',
              content: '请上传列展艺术品。',
              showCancel: false
            })
            return;
          }
        }
        this.lsUploadWorks();
      }
    } else {
      const values = e.detail.value;
      let formValid = true;
      for (const key in values) {
        if (Object.hasOwnProperty.call(values, key)) {
          if (key == 'duties') continue;
          const item = values[key];
          if (!item || item.length === 0) {
            formValid = false;
            this.data.tabStnumArtPeopleMsg[key].showError = true;
          } else {
            this.data.tabStnumArtPeopleMsg[key].value = values[key];
            this.data.tabStnumArtPeopleMsg[key].showError = false;
          }
        }
      }
      if (formValid) {
        if (this.data.activityId == 26) {
          for (const item of this.data.exhibitsOfData) {
            if (item.showPath) {
              if (!item.showLong || !item.showWide) {
                wx.showToast({
                  title: '请填写列展艺术品列展面积',
                  success: () => {
                    this.data.isHandle = false
                  }
                })
                return;
              }
            }
            if (!item.name) {
              wx.showToast({
                title: '请填写列展艺术品名称',
                success: () => {
                  this.data.isHandle = false
                }
              })
              return;
            }
            if (!item.author) {
              wx.showToast({
                title: '请填写列展艺术品作者',
                success: () => {
                  this.data.isHandle = false
                }
              })
              return;
            }
            if (!item.showPath) {
              wx.showToast({
                title: '请上传列展艺术品',
                success: () => {
                  this.data.isHandle = false
                }
              })
              return;
            }
          }
          wx.showModal({
            title: '提示信息',
            content: `您需要支付报名费100元。`,
            confirmText: '支付',
            complete: (res) => {
              if (res.cancel) {
                this.data.isHandle = false
              }
              if (res.confirm) {
                this.wxPay(values)
              }
            }
          })
        } else if (this.data.activityId == 28) {
          for (const item of this.data.exhibitsOfData) {
            if (!item.name) {
              wx.showToast({
                title: '请填写列展艺术品名称',
                success: () => {
                  this.data.isHandle = false
                }
              })
              return;
            }
            if (!item.author) {
              wx.showToast({
                title: '请填写列展艺术品作者',
                success: () => {
                  this.data.isHandle = false
                }
              })
              return;
            }
            if (!item.showPath) {
              wx.showToast({
                title: '请上传列展艺术品',
                success: () => {
                  this.data.isHandle = false
                }
              })
              return;
            }
          }
          wx.showModal({
            title: '提示信息',
            content: `您需要支付报名费90元。`,
            confirmText: '支付',
            complete: (res) => {
              if (res.cancel) {
                this.data.isHandle = false
              }
              if (res.confirm) {
                this.wxPay(values)
              }
            }
          })
        } else if (this.data.activityId == 29 || this.data.activityId == 31) {
          wx.showModal({
            title: '提示信息',
            content: `您需要支付报名费${this.data.payMoney}元。`,
            confirmText: '支付',
            complete: (res) => {
              if (res.cancel) {
                this.data.isHandle = false
              }
              if (res.confirm) {
                this.wxPay(values)
              }
            }
          })
        } else {
          let exhibitsPrice = 0;
          for (const item of this.data.exhibitsOfData) {
            if (item.showPath) {
              if (!item.showLong || !item.showWide) {
                wx.showToast({
                  title: '请填写列展面积',
                })
                return;
              } else {
                const worksAcreage = Util.floatp(
                  Number(item.showLong) * Number(item.showWide),
                  10000
                );
                exhibitsPrice = Util.autoAdd(
                  Util.floatMul(worksAcreage, 200).toString(),
                  exhibitsPrice.toString()
                );
              }
            }
          }
          wx.showModal({
            title: '提示信息',
            content: exhibitsPrice > 0
              ? `您需要支付报名费200元和参展费用${exhibitsPrice}元共计${exhibitsPrice + 200
              }元。`
              : `您需要支付报名费200元。`,
            confirmText: '支付',
            complete: (res) => {
              if (res.cancel) {
                this.data.isHandle = false
              }
              if (res.confirm) {
                this.wxPay(values)
              }
            }
          })
        }
      } else {
        wx.showToast({
          title: '请填写信息',
          success: () => {
            this.data.isHandle = false
          }
        })
        this.setData({
          tabStnumArtPeopleMsg: this.data.tabStnumArtPeopleMsg
        })
      }
    }
  },

  /**
   * 上传艺术品
   */
  lsUploadWorks() {
    const artShowMsgs = this.data.exhibitsOfData.concat(this.data.auctionOfData);
    for (const item of artShowMsgs) {
      item.relateActiveId = this.data.activityId;
      item.worksType = this.data.activityIntro.worksTypeId;
      item.worksItem = item.showMajor;
    }
    const bodyParams = {
      tabStnumArtPeopleMsg: {
        activeId: this.data.activityId,
        stk: app.globalData.userInfo.cardNo,
      },
      tabStnumArtShowMsgs: artShowMsgs,
    };
    wxapi.lsUploadWorks(bodyParams).then(res => {
      if (res.status === 200) {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: `/pages/exchange/detail/detail?activityId=${this.data.activityId}`,
              })
            }
          }
        })
      }
    })
  },

  /**
   * 微信支付
   * @param {*} bodyParams 
   */
  wxPay(values) {
    wx.showLoading({
      title: '支付中...',
    })
    if (this.data.activityId == 26) {
      values.item = this.data.projectOfData.map(item => { return item.id; }).join('|');
    } else if (this.data.activityId == 24) {
      values.item = values.item.join('|');
    }
    values.mostEducation = this.data.educationOfData[this.data.educationIndex];
    const artShowMsgs = this.data.exhibitsOfData.concat(this.data.auctionOfData);
    for (const item of artShowMsgs) {
      item.relateActiveId = this.data.activityId
    }
    const tabStnumArtPeopleMsg = Object.assign(values, { activeId: this.data.activityId, stk: app.globalData.userInfo.cardNo });
    const bodyParams = {
      tabStnumArtPeopleMsg,
      tabStnumArtShowMsgs: artShowMsgs
    }
    const openId = wx.getStorageSync('openId');
    let params = { payType: 'XCX' };
    if (openId) {
      params = Object.assign(params, { openId });
    } else {
      wx.login({
        success: (res) => {
          if (res.code) {
            const jsCode = res.code;
            const headers = {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            }
            wxapi.getMiniOpenId({ jsCode }, headers).then(res => {
              if (res.code === 20000) {
                if (res.data) {
                  wx.setStorageSync('openId', res.data);
                  params = Object.assign(params, { openId: res.data });
                } else {
                  this.data.isHandle = false;
                  wx.hideLoading()
                  wx.showToast({
                    title: '获取openId失败',
                    icon: 'error'
                  })
                }
              } else {
                this.data.isHandle = false;
                wx.hideLoading();
                wx.showModal({
                  title: '提示信息',
                  content: res.message,
                  showCancel: false
                })
              }
            })
          } else {
            this.data.isHandle = false;
            wx.showToast({
              title: '登录失败',
              icon: 'error',
              success: () => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          }
        },
      })
    }
    let url = '';
    if (this.data.activityId == 29 || this.data.activityId == 31) {
      url = '/artEnroll/lsPayEnrollFee'
    } else {
      url = '/artEnroll/wxPayEnrollFee';
    }
    wxapi.wxPayEnrollFee(url, params, bodyParams).then(res => {
      wx.hideLoading();
      if (res.status === 200) {
        this.wxPayment(res.data);
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },

  /**
   * 唤起微信支付
   */
  wxPayment(data) {
    wx.requestPayment({
      nonceStr: data.nonceStr,
      package: data.packageValue,
      paySign: data.paySign,
      signType: data.signType,
      timeStamp: data.timeStamp,
      success: (res) => {
        this.data.isHandle = false;
        wx.showToast({
          title: '支付成功',
          duration: 2000,
          success: () => {
            wx.navigateBack()
          }
        })
      },
      fail: (res) => {
        this.data.isHandle = false;
        this.setData({
          isPaying: false,
          isBargainPaying: false,
          showBargain: false
        })
        if (res.errMsg.indexOf('fail cancel') !== -1) {
          wx.showToast({
            title: '交易取消',
            duration: 2000,
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '订单数据异常',
            duration: 2000,
            icon: 'error'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.activityId) {
      this.data.activityId = options.activityId;
      if (options.activityId == 26) {
        this.data.standby5 = 5;
        this.addExhibits();
      } else if (options.activityId == 28) {
        this.data.standby5 = 6;
        this.addExhibits();
      } else if (options.activityId == 29) {
        this.data.standby5 = 8;
        this.getPayMoney();
        this.getActivityIntro();
      } else if (options.activityId == 31) {
        this.data.standby5 = 8;
        this.setData({
          payMoney: 580
        })
        this.getActivityIntro();
      } else {
        this.getActivityIntro();
      }
      if (options.applyType == 'upload') {
        this.data.applyType = options.applyType;
        this.setData({
          applyType: options.applyType,
          showExhibits: true
        })
        this.artEnrollLsSum();
        this.addExhibits();
      }
      this.setData({
        activityId: options.activityId
      })
      if (app.globalData.token) {
        this.getUserInfo();
      } else {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})